<template>
  <nav class="navbar navbar-inverse navbar-fixed-top"
       role="navigation">
    <div class="navbar-header">
      <button type="button"
              class="navbar-toggle collapsed"
              data-toggle="collapse"
              data-target="#navbar"
              aria-expanded="false"
              aria-controls="navbar">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand"
         id="loginLogo"
         href="#"></a>
    </div>
    <div id="navbar"
         class="collapse navbar-collapse">
      <!-- <ul class="nav navbar-nav">
				<li class="active">
					<a href="javascript:;">首页</a>
				</li>
				<li>
					<a href="javascript:;">机构管理</a>
				</li>
				<li>
					<a href="javascript:;">报备管理</a>
				</li>
			</ul> -->
      <!-- <div style="float:left" class="navbar-nav-switch-wrap">
				<div class="navbar-nav-switch-title">分销助手无需登陆查看楼盘功能</div>
				<i-switch v-model="switch_" @on-change="turnSwitch">
					<span slot="open">开</span>
					<span slot="close">关</span>
				</i-switch >
			</div>
			<div style="float:left" class="navbar-nav-switch-wrap">
				<div class="navbar-nav-switch-title">可重复报备开关</div>
				<i-switch v-model="repeat" @on-change="turnRepeat">
					<span slot="open">开</span>
					<span slot="close">关</span>
				</i-switch >
			</div> -->
      <div class="login-settings">
        <a href="javascript:;"
           class="clearfix">
          <div class="loginImg"></div>
          <div class="loginName">{{accountName}}</div>
          <i class="fa fa-caret-down"></i>
        </a>
        <ul class="login-settings-list">
          <li>
            <a href="javascript:;"
               @click="passwordUpdate()"><i class="fa fa-gear"></i>密码修改</a>
          </li>
          <li>
            <a href="javascript:;"
               @click="loginExit"><i class="fa fa-power-off"></i>退出</a>
          </li>
        </ul>
        <div class="triangle-up"><span class="triangle-up-sm"></span></div>
      </div>
    </div>
    <updatePassword></updatePassword>
  </nav>
</template>
<script>
import updatePassword from "./updatePassword.vue";
export default {
  name: "top",
  data() {
    return {
      accountName: window.localStorage.getItem("1_userName"),
      userId: window.localStorage.getItem("1_userId"),
      switch_: false,
      repeat: false
    };
  },
  components: {
    updatePassword
  },
  mounted: function () {
    this.loginSettingInit();
    this.getCompanyLogo();
    //this.indexForward();
  },
  methods: {
    indexForward() {
      var _self = this;
      var body = {};
      body.params = {};
      var url = this.utilHelper.apiUrl + "/api/getAgencyIndexSwitch";
      _self.$http.post(url, body).then((response) => {
        var data = response.body.resData;
        var result = data.agencyIndexSwitch;
        var repeat = data.repeatReportSwitch;
        if (result) {
          _self.switch_ = result == 1 ? true : false;
          _self.repeat = repeat == 1 ? true : false;
        }
      });
    },
    updatePwd() {
      alert(0);
    },
    passwordUpdate() {
      var _self = this;
      $("#updatePassword").modal("show");
      //_self.isShow = true;
    },
    getCompanyLogo: function () {
      var _self = this;
      var url = _self.utilHelper.apiUrl + "/api/getLogo";
      var params = { logoType: "8" };
      var body = {};
      body.params = params;
      _self.$http.post(url, body).then((response) => {
        var data = response.body;
        if (data.result == "1") {
          //_self.logoUrl = data.resData;
          $("#loginLogo").addClass("top_logo");
          $("#loginLogo").css(
            "backgroundImage",
            "url(" + data.resData.logoUrl + ")"
          );
        } else {
          $("#loginLogo").addClass("top_logo");
        }
      });
    },
    loginSettingInit() {
      $(function () {
        $(document).off("click");
        $(document).on("click", function (event) {
          var $this = $(event.target).parents(".login-settings");
          if ($this.length == 0) {
            $(".login-settings").removeClass("active");
          } else {
            if ($this.hasClass("active")) {
              $this.removeClass("active");
            } else {
              $this.addClass("active");
            }
          }
        });
      });
    },
    settingShow() {
      $(".dropdown-toggle").dropdown("toggle");
    },
    loginExit() {
      var _self = this;
      var params = { userId: _self.userId };
      var body = {};
      body.params = params;
      var url = _self.utilHelper.apiUrl + "/api/loginOut";
      _self.$http.post(url, body).then(
        (response) => {
          var data = response.body.resData;
          var result = data.result;
          var resultMsg = data.resultMsg;
          layer.msg(
            resultMsg,
            {
              time: 1000
            },
            function () {
              window.localStorage.setItem("1_isLogin", null);
              window.localStorage.setItem("1_userId", null);
              _self.$router.replace({
                path: "/login"
              });
            }
          );
        },
        (response) => {}
      );
    },
    turnSwitch: function (status) {
      var _self = this;
      let msg = status ? "开启" : "关闭";
      let recover = status ? false : true;
      layer.confirm(
        "是否" + msg + "免登录查看楼盘",
        {
          btn: ["确定", "取消"],
          cancel() {
            _self.switch_ = recover;
          }
        },
        function (index) {
          var params = { agencyIndexSwitch: status ? 1 : 0 };
          var body = {};
          body.params = params;
          var url = _self.utilHelper.apiUrl + "/api/changeAgencyIndexSwitch";
          _self.$http.post(url, body).then(
            (response) => {
              var data = response.body.resData;
              var result = data.result;
              var resultMsg = data.resultMsg;
              layer.close(index);
              if (result == 1) {
                layer.msg(msg + "成功");
              }
            },
            (response) => {}
          );
        },
        function () {
          _self.switch_ = recover;
        }
      );
    },
    turnRepeat: function (status) {
      var _self = this;
      let msg = status ? "开启" : "关闭";
      let recover = status ? false : true;
      layer.confirm(
        "是否" + msg + "重复报备",
        {
          btn: ["确定", "取消"],
          cancel() {
            _self.repeat = recover;
          }
        },
        function (index) {
          var params = { repeatReportSwitch: status ? 1 : 0 };
          var body = {};
          body.params = params;
          var url = _self.utilHelper.apiUrl + "/api/changeAgencyIndexSwitch";
          _self.$http.post(url, body).then(
            (response) => {
              var data = response.body.resData;
              var result = data.result;
              var resultMsg = data.resultMsg;
              layer.close(index);
              if (result == 1) {
                layer.msg(msg + "成功");
              }
            },
            (response) => {}
          );
        },
        function () {
          _self.repeat = recover;
        }
      );
    }
  }
};
function update_pass() {
  if ($("#oldPassword").val() == null || $("#oldPassword").val() == "") {
    layer.alert("请输入原密码");
    return;
  }

  if ($("#newPassword").val() == null || $("#newPassword").val() == "") {
    layer.alert("请输入新密码");
    return;
  }

  var reg = /^[a-zA-Z0-9~!@#$%^&*()-_+=`{}\[\]:;"'<>?,.\/\\]{8,20}$/;
  if (!reg.test($("#newPassword").val())) {
    layer.msg(
      "密码为长度8-20位的数字、字母、下划线或常用字符(`~!@#$%^&*()-_+={}[]:;\"'<>?,./)",
      {
        time: 3000
      }
    );
    return;
  }
  if ($("#cofirmPassword").val() == null || $("#cofirmPassword").val() == "") {
    layer.alert("请确认新密码");
    return;
  }
  if ($("#newPassword").val() != $("#cofirmPassword").val()) {
    layer.alert("两次密码输入不一致");
    return;
  }
  var data = {
    oldPassword: $("#oldPassword").val(),
    newPassword: $("#newPassword").val(),
    cofirmPassword: $("#cofirmPassword").val(),
    userId: 12
  };
  $.ajax({
    type: "POST",
    url: "${request.contextPath}/assistant/member/updatePasswordDetail.do",
    data: data,
    dataType: "json",
    success: function (rs) {
      /*if(rs.head.success){
				layer.alert(rs.head.message, {
	        	    skin: 'layui-layer-molv' //样式类名
	        	    ,closeBtn: 0
	        	}, function(){
	        		 //刷新
	        		 if(rs.head.success){
	        			 location.href="${request.contextPath}/user/loginOut.do";
	        		 }
	            	
	        	});
			}else{
				layer.alert(rs.head.message);
			}*/
    }
  });
}
</script>
<style>
.navbar-nav-switch-title {
  float: left;
  clear: both;
  margin-right: 5px;
  line-height: 22px;
}
.navbar-nav-switch-wrap {
  margin: 15px 0 0 20px;
}
#navbar-nav-switch {
  width: 45px;
  height: 20px;
  border-radius: 50px;
  position: relative;
  cursor: pointer;
}
#navbar-nav-button {
  width: 18px;
  height: 18px;
  border-radius: 48px;
  position: absolute;
  background: white;
  box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.4);
  cursor: pointer;
}
.open1 {
  background: #ff6600;
}
.open2 {
  top: 1px;
  right: 1px;
}
.close1 {
  background: rgba(255, 255, 255, 0.4);
  border: 1px solid #ff6600;
  border-left: transparent;
}
.close2 {
  left: 1px;
  top: 0;
  /* border:1px solid #FF6600; */
}
</style>